<?php

namespace App\Console\Commands\OpenShop\Nsq;

use App\Models\Cashbook\CashbookModel;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Contract\Message;

class NsqSubOpenShopRmsOrdersUpdateCommand extends Command
{
    protected $signature = 'openshop:nsq-sub-openshop-rms-orders-update';

    protected $description = 'NSQ接受Rms中非当日订单更新通知，如隔天退款，同步隔天离线订单等';

    public function handle()
    {
        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(
                'rms.orders.order.update', 'tiger.openshop.orders.update',
                function (Message $message) {
                    $data = $message->data();
                    //存入redis集合中
                    $date = date('Y-m-d', $data['updatedAt']);
                    $res  = Redis::sAdd(CashbookModel::RMS_ORDERS_UPDATE_DATE, $date);
                    $res2 = Redis::sAdd(CashbookModel::RMS_ORDERS_UPDATE_DATE_SHOP_ID . $date, $data['shopId']);
                    Log::info('NsqRmsOrdersUpdate', ['data' => $data, 'res' => $res, 'res2' => $res2]);
                    $message->done();
                }
            );

    }
}
